home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_include / NET / IP_MASQ.H < prev    next >
C/C++ Source or Header  |  1999-09-17  |  10KB  |  359 lines

  1. /*
  2.  *     IP masquerading functionality definitions
  3.  */
  4.  
  5. #include <linux/config.h> /* for CONFIG_IP_MASQ_DEBUG */
  6. #ifndef _IP_MASQ_H
  7. #define _IP_MASQ_H
  8.  
  9. #ifdef __KERNEL__
  10. #include <linux/types.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/ip.h>
  13. #include <linux/skbuff.h>
  14. #endif /* __KERNEL__ */
  15.  
  16. /*
  17.  * This define affects the number of ports that can be handled
  18.  * by each of the protocol helper modules.
  19.  */
  20. #define MAX_MASQ_APP_PORTS 12
  21.  
  22. /*
  23.  *    Linux ports don't normally get allocated above 32K.
  24.  *    I used an extra 4K port-space
  25.  */
  26.  
  27. #define PORT_MASQ_BEGIN    61000
  28. #define PORT_MASQ_END    (PORT_MASQ_BEGIN+4096)
  29.  
  30. #define MASQUERADE_EXPIRE_TCP     15*60*HZ
  31. #define MASQUERADE_EXPIRE_TCP_FIN  2*60*HZ
  32. #define MASQUERADE_EXPIRE_UDP      5*60*HZ
  33. /* 
  34.  * ICMP can no longer be modified on the fly using an ioctl - this
  35.  * define is the only way to change the timeouts 
  36.  */
  37. #define MASQUERADE_EXPIRE_ICMP      125*HZ
  38.  
  39. #define IP_MASQ_MOD_CTL            0x00
  40. #define IP_MASQ_USER_CTL        0x01
  41.  
  42. #ifdef __KERNEL__
  43.  
  44. #define IP_MASQ_TAB_SIZE    256
  45.  
  46. #define IP_MASQ_F_NO_DADDR          0x0001     /* no daddr yet */
  47. #define IP_MASQ_F_NO_DPORT               0x0002    /* no dport set yet */
  48. #define IP_MASQ_F_NO_SADDR          0x0004    /* no sport set yet */
  49. #define IP_MASQ_F_NO_SPORT          0x0008    /* no sport set yet */
  50.  
  51. #define IP_MASQ_F_DLOOSE          0x0010    /* loose dest binding */
  52. #define IP_MASQ_F_NO_REPLY          0x0080    /* no reply yet from outside */
  53.  
  54. #define IP_MASQ_F_HASHED          0x0100     /* hashed entry */
  55. #define IP_MASQ_F_OUT_SEQ             0x0200    /* must do output seq adjust */
  56. #define IP_MASQ_F_IN_SEQ              0x0400    /* must do input seq adjust */
  57.  
  58. #define IP_MASQ_F_MPORT              0x1000     /* own mport specified */
  59. #define IP_MASQ_F_USER              0x2000    /* from uspace */
  60.  
  61. /*
  62.  *    Delta seq. info structure
  63.  *    Each MASQ struct has 2 (output AND input seq. changes).
  64.  */
  65.  
  66. struct ip_masq_seq {
  67.         __u32        init_seq;    /* Add delta from this seq */
  68.         short        delta;        /* Delta in sequence numbers */
  69.         short        previous_delta;    /* Delta in sequence numbers before last resized pkt */
  70. };
  71.  
  72. /*
  73.  *    MASQ structure allocated for each masqueraded association
  74.  */
  75. struct ip_masq {
  76.         struct ip_masq  *m_link, *s_link; /* hashed link ptrs */
  77.     atomic_t refcnt;        /* reference count */
  78.     struct timer_list timer;    /* Expiration timer */
  79.     __u16         protocol;    /* Which protocol are we talking? */
  80.     __u16        sport, dport, mport;    /* src, dst & masq ports */
  81.     __u32         saddr, daddr, maddr;    /* src, dst & masq addresses */
  82.         struct ip_masq_seq out_seq, in_seq;
  83.     struct ip_masq_app *app;    /* bound ip_masq_app object */
  84.     void        *app_data;    /* Application private data */
  85.     struct ip_masq    *control;    /* Master control connection */
  86.     atomic_t        n_control;    /* Number of "controlled" masqs */
  87.     unsigned      flags;            /* status flags */
  88.     unsigned    timeout;    /* timeout */
  89.     unsigned    state;        /* state info */
  90.     struct ip_masq_timeout_table *timeout_table;
  91. };
  92.  
  93. /*
  94.  *    Timeout values
  95.  *    ipchains holds a copy of this definition
  96.  */
  97.  
  98. struct ip_fw_masq {
  99.         int tcp_timeout;
  100.         int tcp_fin_timeout;
  101.         int udp_timeout;
  102. };
  103.  
  104. union ip_masq_tphdr {
  105.         unsigned char *raw;
  106.         struct udphdr *uh;
  107.         struct tcphdr *th;
  108.         struct icmphdr *icmph;
  109.         __u16 *portp;
  110. };
  111. /*
  112.  *    [0]: UDP free_ports
  113.  *    [1]: TCP free_ports
  114.  *    [2]: ICMP free_ports
  115.  */
  116.  
  117. extern atomic_t ip_masq_free_ports[3];
  118.  
  119. /*
  120.  *    ip_masq initializer (registers symbols and /proc/net entries)
  121.  */
  122. extern int ip_masq_init(void);
  123.  
  124. /*
  125.  *    functions called from ip layer
  126.  */
  127. extern int ip_fw_masquerade(struct sk_buff **, __u32 maddr);
  128. extern int ip_fw_masq_icmp(struct sk_buff **, __u32 maddr);
  129. extern int ip_fw_unmasq_icmp(struct sk_buff *);
  130. extern int ip_fw_demasquerade(struct sk_buff **);
  131.  
  132. /*
  133.  *    ip_masq obj creation/deletion functions.
  134.  */
  135. extern struct ip_masq *ip_masq_new(int proto, __u32 maddr, __u16 mport, __u32 saddr, __u16 sport, __u32 daddr, __u16 dport, unsigned flags);
  136.  
  137. extern void ip_masq_control_add(struct ip_masq *ms, struct ip_masq* ctl_ms);
  138. extern void ip_masq_control_del(struct ip_masq *ms);
  139. extern struct ip_masq * ip_masq_control_get(struct ip_masq *ms);
  140.  
  141. struct ip_masq_ctl;
  142.  
  143. struct ip_masq_hook {
  144.     int (*ctl)(int, struct ip_masq_ctl *, int);
  145.     int (*info)(char *, char **, off_t, int, int);
  146. };
  147.  
  148. extern struct ip_masq *ip_masq_m_tab[IP_MASQ_TAB_SIZE];
  149. extern struct ip_masq *ip_masq_s_tab[IP_MASQ_TAB_SIZE];
  150. extern const char * ip_masq_state_name(int state);
  151. extern struct ip_masq_hook *ip_masq_user_hook;
  152. extern u32 ip_masq_select_addr(struct device *dev, u32 dst, int scope);
  153. /*
  154.  *     
  155.  *    IP_MASQ_APP: IP application masquerading definitions 
  156.  *
  157.  */
  158.  
  159. struct ip_masq_app
  160. {
  161.         struct ip_masq_app *next;
  162.     char *name;        /* name of application proxy */
  163.         unsigned type;          /* type = proto<<16 | port (host byte order)*/
  164.         int n_attach;
  165.         int (*masq_init_1)      /* ip_masq initializer */
  166.                 (struct ip_masq_app *, struct ip_masq *);
  167.         int (*masq_done_1)      /* ip_masq fin. */
  168.                 (struct ip_masq_app *, struct ip_masq *);
  169.         int (*pkt_out)          /* output (masquerading) hook */
  170.                 (struct ip_masq_app *, struct ip_masq *, struct sk_buff **, __u32);
  171.         int (*pkt_in)           /* input (demasq) hook */
  172.                 (struct ip_masq_app *, struct ip_masq *, struct sk_buff **, __u32);
  173. };
  174.  
  175. /*
  176.  *    ip_masq_app initializer
  177.  */
  178. extern int ip_masq_app_init(void);
  179.  
  180. /*
  181.  *     ip_masq_app object registration functions (port: host byte order)
  182.  */
  183. extern int register_ip_masq_app(struct ip_masq_app *mapp, unsigned short proto, __u16 port);
  184. extern int unregister_ip_masq_app(struct ip_masq_app *mapp);
  185.  
  186. /*
  187.  *    get ip_masq_app obj by proto,port(net_byte_order)
  188.  */
  189. extern struct ip_masq_app * ip_masq_app_get(unsigned short proto, __u16 port);
  190.  
  191. /*
  192.  *    ip_masq TO ip_masq_app (un)binding functions.
  193.  */
  194. extern struct ip_masq_app * ip_masq_bind_app(struct ip_masq *ms);
  195. extern int ip_masq_unbind_app(struct ip_masq *ms);
  196.  
  197. /*
  198.  *    output and input app. masquerading hooks.
  199.  *    
  200.  */
  201. extern int ip_masq_app_pkt_out(struct ip_masq *, struct sk_buff **skb_p, __u32 maddr);
  202. extern int ip_masq_app_pkt_in(struct ip_masq *, struct sk_buff **skb_p, __u32 maddr);
  203.  
  204. /*
  205.  *    service routine(s).
  206.  */
  207.  
  208. extern struct ip_masq * ip_masq_out_get(int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port);
  209. extern struct ip_masq * ip_masq_in_get(int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port);
  210.  
  211. extern int ip_masq_listen(struct ip_masq *);
  212.  
  213. static __inline__ struct ip_masq * ip_masq_in_get_iph(const struct iphdr *iph)
  214. {
  215.      const __u16 *portp = (__u16 *)&(((char *)iph)[iph->ihl*4]);
  216.         return ip_masq_in_get(iph->protocol, 
  217.                 iph->saddr, portp[0], 
  218.                 iph->daddr, portp[1]);
  219. }
  220.  
  221. static __inline__ struct ip_masq * ip_masq_out_get_iph(const struct iphdr *iph)
  222. {
  223.      const __u16 *portp  = (__u16 *)&(((char *)iph)[iph->ihl*4]);
  224.         return ip_masq_out_get(iph->protocol, 
  225.                 iph->saddr, portp[0], 
  226.                 iph->daddr, portp[1]);
  227. }
  228.  
  229. extern void ip_masq_put(struct ip_masq *ms);
  230.  
  231.  
  232. extern rwlock_t __ip_masq_lock;
  233.  
  234. #ifdef __SMP__
  235. #define read_lock_bh(lock)     do { start_bh_atomic(); read_lock(lock); \
  236.                     } while (0)
  237. #define read_unlock_bh(lock)    do { read_unlock(lock); end_bh_atomic(); \
  238.                     } while (0)
  239. #define write_lock_bh(lock)    do { start_bh_atomic(); write_lock(lock); \
  240.                     } while (0)
  241. #define write_unlock_bh(lock)    do { write_unlock(lock); end_bh_atomic(); \
  242.                     } while (0)
  243. #else
  244. #define read_lock_bh(lock)    start_bh_atomic()
  245. #define read_unlock_bh(lock)    end_bh_atomic()
  246. #define write_lock_bh(lock)    start_bh_atomic()
  247. #define write_unlock_bh(lock)    end_bh_atomic()
  248. #endif 
  249. /*
  250.  *
  251.  */
  252.  
  253. /*
  254.  *    Debugging stuff
  255.  */
  256.  
  257. extern int ip_masq_get_debug_level(void);
  258.  
  259. #ifdef CONFIG_IP_MASQ_DEBUG
  260. #define IP_MASQ_DEBUG(level, msg...) do { \
  261.     if (level <= ip_masq_get_debug_level()) \
  262.         printk(KERN_DEBUG "IP_MASQ:" ## msg); \
  263.     } while (0)
  264. #else    /* NO DEBUGGING at ALL */
  265. #define IP_MASQ_DEBUG(level, msg...) do { } while (0)
  266. #endif
  267.  
  268. #define IP_MASQ_INFO(msg...) \
  269.     printk(KERN_INFO "IP_MASQ:" ## msg)
  270.  
  271. #define IP_MASQ_ERR(msg...) \
  272.     printk(KERN_ERR "IP_MASQ:" ## msg)
  273.  
  274. #define IP_MASQ_WARNING(msg...) \
  275.     printk(KERN_WARNING "IP_MASQ:" ## msg)
  276.  
  277.  
  278. /*
  279.  *    /proc/net entry
  280.  */
  281. extern int ip_masq_proc_register(struct proc_dir_entry *);
  282. extern void ip_masq_proc_unregister(struct proc_dir_entry *);
  283. extern int ip_masq_app_getinfo(char *buffer, char **start, off_t offset, int length, int dummy);
  284.  
  285. /*
  286.  *    skb_replace function used by "client" modules to replace
  287.  *    a segment of skb.
  288.  */
  289. extern struct sk_buff * ip_masq_skb_replace(struct sk_buff *skb, int pri, char *o_buf, int o_len, char *n_buf, int n_len);
  290.  
  291. /*
  292.  * masq_proto_num returns 0 for UDP, 1 for TCP, 2 for ICMP
  293.  */
  294.  
  295. static __inline__ int masq_proto_num(unsigned proto)
  296. {
  297.    switch (proto)
  298.    {
  299.       case IPPROTO_UDP:  return (0); break;
  300.       case IPPROTO_TCP:  return (1); break;
  301.       case IPPROTO_ICMP: return (2); break;
  302.       default:           return (-1); break;
  303.    }
  304. }
  305.  
  306. static __inline__ const char *masq_proto_name(unsigned proto)
  307. {
  308.     static char buf[20];
  309.     static const char *strProt[] = {"UDP","TCP","ICMP"};
  310.     int msproto = masq_proto_num(proto);
  311.  
  312.     if (msproto<0||msproto>2)  {
  313.         sprintf(buf, "IP_%d", proto);
  314.         return buf;
  315.     }
  316.         return strProt[msproto];
  317. }
  318.  
  319. enum {
  320.     IP_MASQ_S_NONE = 0,
  321.     IP_MASQ_S_ESTABLISHED,
  322.     IP_MASQ_S_SYN_SENT,
  323.     IP_MASQ_S_SYN_RECV,
  324.     IP_MASQ_S_FIN_WAIT,
  325.     IP_MASQ_S_TIME_WAIT,
  326.     IP_MASQ_S_CLOSE,
  327.     IP_MASQ_S_CLOSE_WAIT,
  328.     IP_MASQ_S_LAST_ACK,
  329.     IP_MASQ_S_LISTEN,
  330.     IP_MASQ_S_UDP,
  331.     IP_MASQ_S_ICMP,
  332.     IP_MASQ_S_LAST
  333. };
  334.  
  335. struct ip_masq_timeout_table {
  336.     atomic_t refcnt;
  337.     int scale;
  338.     int timeout[IP_MASQ_S_LAST+1];
  339. };
  340.  
  341. static __inline__ void ip_masq_timeout_attach(struct ip_masq *ms, struct ip_masq_timeout_table *mstim)
  342. {
  343.     atomic_inc (&mstim->refcnt);
  344.     ms->timeout_table=mstim;
  345. }
  346.  
  347. static __inline__ void ip_masq_timeout_detach(struct ip_masq *ms)
  348. {
  349.     struct ip_masq_timeout_table *mstim = ms->timeout_table;
  350.  
  351.     if (!mstim)
  352.         return;
  353.     atomic_dec(&mstim->refcnt);
  354. }
  355.  
  356. #endif /* __KERNEL__ */
  357.  
  358. #endif /* _IP_MASQ_H */
  359.